<?php
// @formatter:off
/**
 * @file c.rules.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-22 17:58:11 +0000 (Sat, 22 Jun 2013) $
 *
 * $Id: c.rules.php 83 2013-06-22 17:58:11Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/themes/default/controlers/contents/c.rules.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_RULE_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcThemeAssigns;
	global $wcGroup;
	global $wcRulesHolder;
	global $wctDefaults;
	global $wcPageCache;

	$wcPageCache->anyRule();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minRuleInPage = $wctDefaults["max-per-page"]["rules"] * ($page - 1);
	$maxRuleInPage = $minRuleInPage + $wctDefaults["max-per-page"]["rules"];

	$wcThemeAssigns["PATH"] = "All Rules (page {$page})";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.rules.html";

	//
	// Selecting only top rules in range.
	$rules = array();
	$items = $wcRulesHolder->items($wcGroup);
	$ruleCount = 0;
	uasort($items, "WCRule::NameSort");
	foreach($items as $rule) {
		if($rule->ok()) {
			if($ruleCount >= $minRuleInPage && $ruleCount < $maxRuleInPage) {
				$rules[] = $rule;
			}
			$ruleCount++;
		}
	}

	wcSetPager(requestParams(true, $wcAction, array('page')), ceil($ruleCount / $wctDefaults["max-per-page"]["rules"]), $page);

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	foreach($rules as $rule) {
		$name = $rule->name;
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = wcGetRulePreviewAssigns($rule);
	}
	ksort($lists);

	$wcThemeAssigns["RULES-LISTS"] = $lists;

	if($wcProfile->allowedTo(WC_PERM_RULE_ADD)) {
		$wcThemeAssigns["RULES-CREATE"] = true;
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list rules";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>
